<template>
  <div class="main-left">
    <div class="content">
      <div
        v-if="patrolAlarmbyRearerList.length"
        class="content-box"
        v-infinite-scroll="load"
        :infinite-scroll-disabled="disabled"
        :infinite-scroll-immediate="false"
        infinite-scroll-distance="10"
      >
        <div
          v-for="(item, index) in patrolAlarmbyRearerList"
          :key="index"
          class="card"
          @click="clickItem(item.id)"
          :class="{ taskPoint: selectedFid == item.id }"
        >
          <div class="card-title">
            <span>{{ item.wens_rearer_name }}</span>
            <el-tag type="success" effect="dark" v-if="item.wens_task_status == '2'">
              {{ item?.wens_task_status_title }}
            </el-tag>
            <el-tag type="warning" effect="dark" v-else>
              {{ item?.wens_task_status_title }}
            </el-tag>
          </div>
          <div class="card-span">
            告警事项：{{ item.wens_alarm_level == 1 ? 'I' : 'II' }}级-{{
              item.wens_event_type_title
            }}告警
          </div>
          <div class="card-span">告警详情：{{ item.wens_alarm_content || '-' }}</div>
          <div class="card-span">告警时间：{{ item.wens_datetime }}</div>
          <div class="card-span">处理情况：{{ item.wens_alarm_content || '-' }}</div>
        </div>
      </div>
      <el-empty description="暂无数据" v-else />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref, reactive, computed, watch } from 'vue'
import { getPatrolAlarmByRearerV2 } from '@/api/api'

const selectedFid = ref('')

const props = defineProps({
  rearerId: {
    type: String,
    default: ''
  }
})

watch(
  () => props.rearerId,
  (newValue, oldValue) => {
    if (newValue) {
      listParams.value.pageNum = 1
      patrolAlarmbyRearerList.value.length = 0
      fetchPatrolAlarmByRearer()
    }
  },
  { immediate: true }
)

const emit = defineEmits(['change'])
const clickItem = (fid) => {
  selectedFid.value = fid
  emit('change', fid)
}

const patrolAlarmbyRearerList = ref([]) // 获取服务部告警任务列表
const listParams = ref({
  pageNum: 1,
  pageSize: 20,
  total: 0,
  loading: false,
  finished: false
})

const disabled = computed(() => {
  return listParams.value.finished
})
const load = () => {
  if (listParams.value.finished == false) {
    listParams.value.pageNum++
    fetchPatrolAlarmByRearer()
    console.log('加载更多')
  }
}

// 获取根据养户id 获取告警信息列表
const fetchPatrolAlarmByRearer = async () => {
  try {
    const res = await getPatrolAlarmByRearerV2({
      rearerId: props.rearerId,
      pageSize: listParams.value.pageSize,
      pageNo: listParams.value.pageNum
    })
    if (res && res.data) {
      if (res.data.rows.length > 0) {
        patrolAlarmbyRearerList.value.push(...res.data.rows)
        if (res.data.lastPage) {
          listParams.value.finished = true
        } else {
          listParams.value.finished = false
        }
      } else {
        listParams.value.finished = false
      }
    }
  } catch (error) {
    console.error('获取告警信息列表失败:', error)
  }
}
</script>

<style scoped lang="scss">
.taskPoint {
  border: 2px solid blue !important;
}

.red {
  color: red;
}

.main-left {
  height: 100%;
  width: 350px;
  margin-right: 50px;

  .content {
    height: 100%;
    display: flex;
    flex-direction: column;

    &-title {
      font-weight: bold;
      font-size: 16px;
      margin-bottom: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    &-box {
      overflow-y: auto;
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;

      .card {
        border-radius: 5px;
        width: 100%;
        margin-bottom: 10px;
        padding: 10px;
        line-height: 30px;
        background-image: linear-gradient(to right, #002f67, #0049a0);

        &-title {
          display: flex;
          justify-content: space-between;
          font-size: 14px;
          font-weight: bold;
          margin-bottom: 5px;
        }

        &-span {
          line-height: 24px;
        }
      }
    }
  }
}
</style>
